package com.example.secondhandspringboot.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.secondhandspringboot.domain.entity.School;
import com.example.secondhandspringboot.domain.vo.SchoolVO;
import com.example.secondhandspringboot.results.Result;
import com.example.secondhandspringboot.service.ISchoolService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 周在林
 * @since 2024-03-30
 */
@RestController
@RequestMapping("/school")
@CrossOrigin
@Slf4j
@RequiredArgsConstructor
public class SchoolController {

    private final ISchoolService schoolService;

    /**
     * 获取学校列表
     *
     * @param queryString
     * @return
     */
    @GetMapping("/getSchoolNames")
    public Result<List<SchoolVO>> getSchoolNames(@RequestParam String queryString) {
        List<SchoolVO> schools = schoolService.list(new LambdaQueryWrapper<School>().like(School::getSchoolName, queryString)).stream()
                .map(school -> new SchoolVO(school.getId(),school.getSchoolName()))
                .toList();
        log.info("获取学校列表:"+schools);
        return Result.success(schools);
    }
}
